// Project: Salient
// http://salient.codeplex.com
// 
// Copyright 2010, Sky Sanders <sky at skysanders.net>
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://salient.codeplex.com/license
// 
// Date: April 25 2010 

#region

using System.Security.Principal;
using System.Web;

#endregion

namespace Salient.Web.Security
{
    ///<summary>
    ///</summary>
    public class AccessControlPrincipal : IPrincipal
    {
        private readonly AccessControlIdentity _id;
        private readonly IPrincipal _innerPrincipal;

        ///<summary>
        ///</summary>
        ///<param name="application"></param>
        public AccessControlPrincipal(HttpApplication application)
        {
            _innerPrincipal = application.User;
            _id = new AccessControlIdentity(_innerPrincipal.Identity);
        }

        ///<summary>
        ///</summary>
        public bool Authenticated
        {
            get { return _id.Authenticated; }
            set { _id.Authenticated = value; }
        }

        #region IPrincipal Members

        /// <summary>
        /// Determines whether the current principal belongs to the specified role.
        /// </summary>
        /// <returns>
        /// true if the current principal is a member of the specified role; otherwise, false.
        /// </returns>
        /// <param name="role">The name of the role for which to check membership. 
        ///                 </param>
        public bool IsInRole(string role)
        {
            return _innerPrincipal.IsInRole(role);
        }

        /// <summary>
        /// Gets the identity of the current principal.
        /// </summary>
        /// <returns>
        /// The <see cref="T:System.Security.Principal.IIdentity"/> object associated with the current principal.
        /// </returns>
        public IIdentity Identity
        {
            get { return _id; }
        }

        #endregion
    }
}